package de.koller.gravity.mech.particle;

import de.koller.gravity.mech.particle.creator.IParticleCreator;
import de.koller.gravity.mech.particle.environment.IParticleEnvironment;
import de.koller.gravity.mech.particle.render.IParticleRenderer;
import de.koller.oceanFramework.util.OceanSet;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class ParticleEngine {
	public Random rnd = new Random();
	
	private long engineStep = 0;
	
	public final OceanSet<Particle> particles = new OceanSet<>();
	
	public final List<IParticleCreator> creators = new ArrayList<>();
	public final List<IParticleRenderer> renderers = new ArrayList<>();
	public final List<IParticleEnvironment> environments = new ArrayList<>();
	
	public void step() {
		engineStep++;
		
		for( IParticleCreator c : creators ) {
			Set<Particle> cp = c.creationStep( this );
			for( Particle p : cp ) {
				p.creationTime = engineStep;
				particles.add( p );
			}
		}
		
		particles.update();
		
		for( IParticleEnvironment e : environments )
			for( Particle p : particles )
				e.stepParticle( p, this );
		
		particles.update();
	}
	
	public void renderFast( Graphics2D g ) {
		for( IParticleRenderer r : renderers )
			for( Particle p : particles )
				r.renderParticle( p, g, this );
	}
	
	public void qualityRendering( Graphics2D g, List<Particle> sortedParticles ) {
		for( IParticleRenderer r : renderers )
			for( Particle p : sortedParticles )
				r.renderParticle( p, g, this );
	}
	
	public long getEngineStep() {
		return engineStep;
	}
	
}
